<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/05/18 22:20:31 $)
// $RCSfile: impex.departments.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Return the Total Pages for this Plugin
*/
function getPluginTotalPages5()
{
	return 1;
}

/**
* Run the specific page for plugin
*/
function runPluginPage5($page)
{
	global $impexDB, $_IMPEXHASH;

	$departmentlist = array();
	$impexDB->query("SELECT * FROM `perlDesk_departments`;");
	$index = 0;
	while ($impexDB->nextRecord())
	{
		if (empty($impexDB->Record["id"]))
		{
			$_depid = $index;
		} else {
			$_depid = $impexDB->Record["id"];
		}
		$departmentlist[$_depid] = $impexDB->Record;
		$index++;
	}

	foreach ($departmentlist as $key=>$val)
	{
		$_departmentid = _impexInsertDepartment($val["level"], iif($val["internal"]=="1", SWIFTPRIVATE, SWIFTPUBLIC));

		displayStatus("Imported Department: ".htmlspecialchars($val["level"]));

		$_IMPEXHASH["departmentpointer"][$val["level"]] = $_departmentid;
		$_IMPEXHASH["departmentidpointer"][$val["id"]] = $_departmentid;
	}

	// Assignments
	$depassigns = array();
	$impexDB->query("SELECT * FROM `perlDesk_staffaccess`;");
	while ($impexDB->nextRecord())
	{
		if ($impexDB->Record["dep_id"] > 0)
		{
			$depassigns[$impexDB->Record["dep_id"]] = $impexDB->Record["staff_id"];
		}
	}

	// Insert the assignments
	foreach ($depassigns as $key=>$val)
	{
		$newdepartmentid = intval($_IMPEXHASH["departmentpointer"][$key]);
		$newstaffid = intval($_IMPEXHASH["staffpointer"][$val]);

		if (empty($newdepartmentid) || empty($newstaffid))
		{
			displayStatus("<font color='red'>WARNING:</font> Import failed when importing staff or department ($newdepartmentid - $newstaffid)");
		}

		$_staffassignid = _impexInsertStaffAssigns($newdepartmentid, $newstaffid);

		displayStatus("Assigned Staff (ID: ". $newstaffid .") to Department (ID: ". $newdepartmentid .")");
	}

	return true;
}

?>